﻿;(function($) {
	$.fn.listTable = function(options) {
		var self = this,
			local_url = window.location.search,
			settings = {
				url: $(self).attr('data-acturi')
			}
		if(options) {
			$.extend(settings, options);
		}
		
		//Edit
		$('span[data-tdtype="edit"], span[data-tdtype="textarea"]', $(self)).live('click', function() {
			var s_val   = $(this).text(),
				s_name  = $(this).attr('data-field'),
				s_id    = $(this).attr('data-id'),
				s_extra = $(this).attr('data-extra'),
				s_type  = $(this).attr('data-tdtype');
			var width;	
			var inputHtml;
			var handler = function(cancelled) {
				$(this).prev('span').show();
				if (!cancelled) {
					$(this).prev('span').text($(this).val());
				
					if($(this).val() != s_val) {
						$.getJSON(settings.url + (s_extra ? "&" + s_extra : ''), {id:s_id, field:s_name, val:$(this).val()}, function(result){
							if(result.status == 0) {
								$.jBox.tip('Data modification fails', 'error');
								$('span[data-field="'+s_name+'"][data-id="'+s_id+'"]').text(s_val);
								return;
							}
						});
					}
				}
				$(this).remove();
			}
			
			if (s_type == 'edit') {
				inputHtml = '<input type="text" class="lt_input_text" value="'+s_val+'" />';
				width   = $(this).width() + 20;
			}
			else {
				inputHtml = '<textarea style="position:absolute;z-index:2;">' + s_val + '</textarea>';
				width   = $(this).width();
			}
			
			
			$(inputHtml).width(width).focusout(function(){
				handler.call(this, false);
			}).keypress(function(e){
				if (e.keyCode == 13) {
					handler.call(this, false);
				}
				else if (e.keyCode == 27) {
					handler.call(this, true);
				}
			}).insertAfter($(this)).focus().select();
			$(this).hide();
			return false;
		});
		
		//select
		$('span[data-tdtype="select"]', $(self)).live('click', function() {
			var s_val    = $(this).attr('data-value'),
				s_text   = $(this).text(),
				s_name   = $(this).attr('data-field'),
				s_id     = $(this).attr('data-id'),
				s_extra =  $(this).attr('data-extra'),
				s_source = $(this).attr('data-source');
				
			eval('var arraySource = ' + s_source);
			var select = "<select>";
			for (var i=0; i < arraySource.length; i++) {
				var selected = '';
				if (arraySource[i].value == s_val) {
					selected = " selected";
				}
				select += "<option value='" + arraySource[i].value + "'" + selected + ">" + arraySource[i].label + "</option>";
			}
			select += "</select>";
			
			var handler = function(cancelled) {
				$(this).prev('span').show();
				if (!cancelled) {
					$(this).prev('span').attr('data-value', $(this).val()).text($(this).find('option:selected').text());
					if($(this).val() != s_val) {
						$.getJSON(settings.url + (s_extra ? "&" + s_extra : ''), {id:s_id, field:s_name, val:$(this).val()}, function(result){
							if(result.status == 0) {
								$.jBox.tip('Data modification fails', 'error');
								$('span[data-field="'+s_name+'"][data-id="'+s_id+'"]').text(s_text).attr('data-value', s_val);
								return;
							}
						});
					}
				}
				$(this).remove();
			}
			
			$(select).focusout(function(){
				handler.call(this, false);
			}).keypress(function(e){
				if (e.keyCode == 13) {
					handler.call(this, false);
				}
				else if (e.keyCode == 27) {
					handler.call(this, true);
				}
			}).insertAfter($(this)).focus().select();
			$(this).hide();
			return false;
		});
		
		//Toggle
		$('img[data-tdtype="toggle"]', $(self)).live('click', function() {
			var img    = this,
				s_val  = ($(img).attr('data-value') == 0) ? 1 : 0,
				s_name = $(img).attr('data-field'),
				s_id   = $(img).attr('data-id'),
				s_extra = $(img).attr('data-extra'),
				s_src  = $(img).attr('src');
			if(!s_name) s_name = 'status';
			$.getJSON(settings.url + (s_extra ? "&" + s_extra : ''), {id:s_id, field:s_name, val:s_val}, function(result){
				if(result.status == 1) {
					if(s_src.indexOf('disabled')>-1) {
						$(img).attr({'src':s_src.replace('disabled','enabled'),'data-value':s_val});
					} else {
						$(img).attr({'src':s_src.replace('enabled','disabled'),'data-value':s_val});
					}
				}
				else {
					$.jBox.tip('Status modification fails', 'error');
				}
			});
			return false;
		});
	};
})(jQuery);